package util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import dao.Chapter;



public class MakeHtml {

	/**
	 * @param args
	 */
	public boolean MakeFile(String path,String template,Chapter chapter,String author){
		try {
			FileInputStream inputStream = new FileInputStream(template);
			int templateLen = inputStream.available(); 
			byte[] templateContext = new byte[templateLen];
			inputStream.read(templateContext, 0, templateLen);
			inputStream.close();
			StringBuilder builder = new StringBuilder(new String(templateContext));
			int mark1 = builder.indexOf("##title1#");
			builder.replace(mark1, mark1+9, chapter.getName());
			
			int mark2 = builder.indexOf("##title#");
			builder.replace(mark2, mark2+8, chapter.getName());
			
			int mark3 = builder.indexOf("##author#");
			builder.replace(mark3, mark3+9, author);
			
			int mark5 = builder.indexOf("##time#");
			DateFormat dateFormat1 = new SimpleDateFormat("yyyy-M-d hh:M:s");
			builder.replace(mark5, mark5+7, dateFormat1.format(chapter.getDemand()));
			
			int mark4 = builder.indexOf("##content#");
			builder.replace(mark4, mark4+10, chapter.getStructurechart());
						
			char[] source = builder.toString().toCharArray();
			FileOutputStream outputStream = new FileOutputStream(path);
			OutputStreamWriter writer1 = new OutputStreamWriter(outputStream,"UTF-8");
			writer1.write(source);
			writer1.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	public static void main(String[] args) {
//	    StringBuffer idString = new StringBuffer("stu");
//	    idString.append(System.currentTimeMillis());
//	    System.out.println(idString.substring(0, 10));
	    String string = "";
	    try {
		string = new String(("涓栫晫浠庝綍鑰屾潵").getBytes("gbk"),"gb2312");
		//string = new String(("认识自己").getBytes("utf-8"),"gb2312");
	    } catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	    System.out.println(string);
	}

}
